﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Input;

namespace Milkify.Input
{
    public class GenericKeyGestureConverter : TypeConverter
    {
        private static TypeConverter sKeyConverter = TypeDescriptor.GetConverter(typeof(Key));
        private static TypeConverter sModifierKeysConverter = TypeDescriptor.GetConverter(typeof(ModifierKeys));

        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            return (sourceType == typeof(string));
        }

        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            return (destinationType == typeof(string) && context.Instance != null);
        }

        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            if (value != null && value is string)
            {
                string source = ((string)value).Trim();
                if (source == string.Empty)
                    return new GenericKeyGesture(Key.None);

                int plusIndex = source.IndexOf("+");
                if (plusIndex >= 0)
                {
                    // Modifier key + key.
                    string modifierKeysPart = source.Substring(0, plusIndex).Trim();
                    string keyPart = source.Substring(plusIndex + 1).Trim();
                    ModifierKeys modifierKeys = (ModifierKeys)sModifierKeysConverter.ConvertFrom(context, culture, modifierKeysPart);
                    Key key = (Key)sKeyConverter.ConvertFrom(context, culture, keyPart);
                    return new GenericKeyGesture(key, modifierKeys);
                }
                else
                {
                    // No modifier key.
                    Key key = (Key)sKeyConverter.ConvertFrom(context, culture, source);
                    return new GenericKeyGesture(key);
                }
            }
            return base.ConvertFrom(context, culture, value);
        }

        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string) && value is GenericKeyGesture)
            {
                if (value == null)
                    return string.Empty;

                GenericKeyGesture keyGesture = (GenericKeyGesture)value;
                if (keyGesture.Key == Key.None)
                    return string.Empty;

                string modifierKeysPart = (string)sModifierKeysConverter.ConvertTo(context, culture, keyGesture.Modifiers, typeof(string));
                string keyPart = (string)sKeyConverter.ConvertTo(context, culture, keyGesture.Key, typeof(string));
                string result = keyPart;
                if (!string.IsNullOrEmpty(modifierKeysPart))
                    result = modifierKeysPart + "+" + keyPart;
                return result;
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }
    }
}
